cc.Class({
    extends: cc.Component,
    properties: {
        mpf:cc.Prefab,
        body:[cc.Node],
    },
    onLoad () {
        this.speed=200;
        this.pos=[];
        this.posIndex=[];
        this.type=0;
        this.blood=[10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000];
        this.getNewPos();
        for(var i=0;i<this.blood.length;i++){
            this.posIndex.push(0);
            var m=cc.instantiate(this.mpf);
            var p=cc.v2(this.toPos.x,this.toPos.y-i*m.height*0.5);
            m.setPosition(p);
            this.node.addChild(m);
            this.body.push(m);
        }
        this.getNewPos();
    },
    getNewPos(){
        var np=cc.v2(Math.random()*1280-640,Math.random()*720-360);
        if(this.toPos&&cc.PointUtils.getDistance(this.toPos,np)<100)this.getNewPos();
        else {
            this.toPos=np;
            this.pos.push(this.toPos);
            // if(this.pos.length>this.body.length)this.pos.shift();
        }
    },
    mov(){
        for(var j=0;j<this.body.length;j++){
            console.log(">>>"+j);
            var cp=this.body[j].getPosition();
            var np=this.pos[this.posIndex[j]];
            var vp=cc.PointUtils.getXySpeed(cp,np,this.speed);
            this.body[j].rotation=cc.PointUtils.getAngle(cp,np);
            this.body[j].x+=vp.x;
            this.body[j].y+=vp.y;
            if(cc.PointUtils.getDistance(cp,np)<this.speed){
                this.posIndex[j]++;
            }
        }
        if(cc.PointUtils.getDistance(this.body[0].getPosition(),this.toPos)<=this.speed)this.getNewPos();
    },
    moving(){
        var np=this.toPos;
        for(var i=0;i<this.body.length;i++){ 
            var cp=this.body[i].getPosition();
            var vp=cc.PointUtils.getXySpeed(cp,np,this.speed);
            if(i==0||cc.PointUtils.getDistance(cp,this.body[i-1].getPosition())>this.body[i].width*0.5){
                this.body[i].rotation=cc.PointUtils.getAngle(cp,np);
                np=cp;
                this.body[i].x+=vp.x;
                this.body[i].y+=vp.y;
            }
        }
        if(cc.PointUtils.getDistance(this.body[0].getPosition(),this.toPos)<=this.speed)this.getNewPos();
    },
    start () {},
    update (dt) {
        this.mov();
    },
});
